#include <stdio.h>
#include <unistd.h>

#include <math.h>
#include "cmsis_os2.h"

#include "ohos_types.h"
#include "iot_errno.h"
#include "iot_gpio.h"
#include "iot_i2c.h"
#include "iot_gpio_ex.h"

#include "peripheral.h"
#include "bh1750.h"

static void BH1750Start(void)
{
    uint8_t send_data[1] = { 0x10 };
    IoTI2cWrite(BH1750_I2C_IDX, (BH1750_I2C_ADDR << 1)|0x00, send_data, 1); 
}


/***************************************************************
* 函数名称: BH1750Init
* 说    明: 写命令初始化BH1750
* 参    数: 无
* 返 回 值: 无
***************************************************************/
void BH1750Init(void)
{
    uint8_t send_data[1] = { 0x01 };
    IoTI2cWrite(BH1750_I2C_IDX, (BH1750_I2C_ADDR << 1)|0x00, send_data, 1); 
}

/***************************************************************
* 函数名称: BH1750ReadData
* 说    明: 测量光照强度
* 参    数: 无
* 返 回 值: 无
***************************************************************/
void BH1750ReadData(float *lux)
{
    BH1750Start(); // 启动传感器采集数据
    usleep(180000);
    uint8_t recv_data[2] = { 0 };
    IoTI2cRead(BH1750_I2C_IDX, (BH1750_I2C_ADDR<<1)|0x01, recv_data, 2);   // 读取传感器数据
    if (lux != NULL)
        *lux = (float)(((recv_data[0]<<8) + recv_data[1])/1.2);
}


